package com.khyr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.khyr.dao.DictDataDao;
import com.khyr.entity.DictData;
import com.khyr.service.DictDataService;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @className: DictDataServiceImpl
 * @author: Zyl
 * @date: 2025/3/11 21:20
 * @Version: 1.0
 * @description:
 */
@Service
public class DictDataServiceImpl implements DictDataService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DictDataDao dictDataDao;
    @Override
    public R<List<DictData>> getByDictType(String dictType) {
        // 先从redis中获取数据
        List<DictData> dictData = (List<DictData>) redisTemplate.opsForValue().get("dictData::" + dictType);
        if (dictData != null) {
            return new R<List<DictData>>(200, "查询成功", dictData);
        }
        QueryWrapper<DictData> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type", dictType);
        wrapper.eq("status", "0");
        dictData = dictDataDao.selectList(wrapper);
        redisTemplate.opsForValue().set("dictData::" + dictType, dictData, 60 * 60 * 24, TimeUnit.SECONDS);
        return new R<List<DictData>>(200, "查询成功", dictData);

    }
}
